package com.example.woke;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.Intentnet;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class ZhuanActivity extends Activity implements OnClickListener {

	private EditText mEzhuan1,mEzhuan2,mEzhuan3;
	private MyApp application;
	private AlertDialog alertDialog;
	private Intentnet net;
	private View mBar;
	private String money;
	private String note;
	private String tmoney;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhuan);
		
		application = (MyApp) getApplication();
		intview();
		
	}

	private void intview() {
		net = new Intentnet();
		findViewById(R.id.zhuan_image_back).setOnClickListener(this);
		mEzhuan1 = (EditText) findViewById(R.id.edit_zhuan1);
		mEzhuan2 = (EditText) findViewById(R.id.edit_zhuan2);
		mEzhuan3 = (EditText) findViewById(R.id.edit_zhuan3);
		findViewById(R.id.text_zhuan_ok).setOnClickListener(this);
		mBar = findViewById(R.id.zhuan_progress_bar);
	}
	
	private void aralogid1(String info,String kll,final int ppp){
		   alertDialog = new AlertDialog.Builder(ZhuanActivity.this). 
	                setTitle(info). 
	                setMessage(kll). 
//	                setIcon(R.drawable.ic_launcher). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    Intent intent = new Intent();
	                    		intent.setClass(ZhuanActivity.this, PswchangeActivity.class);
	                    		intent.putExtra("name", 0);
	                    		startActivity(intent);
	                    		alertDialog.dismiss();
	                    } 
	                }). 
	                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	alertDialog.dismiss();
	                        // TODO Auto-generated method stub  
	                    } 
	                }).create(); 
	        alertDialog.show(); 
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(requestCode == 700 && resultCode == 600){
			paymoney();
		}
		
	}
	
	private void paymoney() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "order");
		params.put("a", "transfer");
		params.put("userid", application.getDatas_load().getId());
//		params.put("touserid", userid);
		params.put("money", note);
		params.put("note", tmoney);
		params.put("phone", money);
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("转账", "成功"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
//						JSONObject object = response.getJSONObject("data");
//						JSONObject jsonObject = object.getJSONObject("data");
						Toast.makeText(ZhuanActivity.this, info, Toast.LENGTH_SHORT).show();
//						intent.setClass(PaytofriendActivity.this, ZcaipayokActivity.class);
//						intent.putExtra("user_nicename", "user_nicename");
//						startActivity(intent);
						finish();
					}else{
						Toast.makeText(ZhuanActivity.this, info, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.zhuan_image_back:
			finish();
			break;
			
		case R.id.text_zhuan_ok:
			money = ""+mEzhuan1.getText().toString().trim();
			note = ""+mEzhuan2.getText().toString().trim();
			tmoney = mEzhuan3.getText().toString().trim();
			String tmoneys = mEzhuan2.getText().toString().trim();
			boolean textline = false;//�ж�С���λ���Ƿ񳬹�
			int indexOf = tmoneys.indexOf('.');
			int length = tmoneys.length();
			if(indexOf > -1){
				if((length-indexOf - 1) > 2){
//					mEmoney.setText(tmoney.substring(0, tmoney.length() - 1));0
					textline = true;
				}
			}
			if(application.getDatas_load ()==null)return;
			if(null == application.getDatas_load().getPay_pwd() || application.getDatas_load().getPay_pwd().equals("")){
				Toast.makeText(ZhuanActivity.this, "请设置支付密码", Toast.LENGTH_SHORT).show();
				aralogid1("还未设置支付密码","前往设置？",1);
			}else{
				money = ""+mEzhuan1.getText().toString().trim();
				if(money.equals("") || null == money){
					Toast.makeText(ZhuanActivity.this, "请输入会员手机号！", Toast.LENGTH_SHORT).show();
				}else if(net.isMobile(money) == false){
					Toast.makeText(ZhuanActivity.this, "非法的手机号", Toast.LENGTH_SHORT).show();
				}else if(money.equals(application.getDatas_load().getUser_login())){
					Toast.makeText(ZhuanActivity.this, "不可以向自己转账", Toast.LENGTH_SHORT).show();
				}else if(note.equals("") || null == note){
					Toast.makeText(ZhuanActivity.this, "请输入转账金额!", Toast.LENGTH_SHORT).show();
				}else if(textline == true){
						Toast.makeText(ZhuanActivity.this, "金额小数点后最多2位", Toast.LENGTH_SHORT).show();
				}else{
					Intent intent1 = new Intent(ZhuanActivity.this,CheckpaypswActivity.class);
					intent1.putExtra("paymoney", note);
					startActivityForResult(intent1, 700);
				}
			}
			break;

		default:
			break;
		}
	}
}
